<?php

class DB_Query {
	var $query;
	var $query_vars;
	var $queried_object;
	var $queried_object_id;

	var $posts;
	var $post_count = 0;
	var $current_post = -1;
	var $post;
	
	var $user;

	var $is_home = false;
	var $is_page = false;
	var $is_login = false;
	var $is_register = false;
	var $is_services = false;
	var $is_about = false;
	var $is_contact = false;
	var $is_submit = false;
	var $is_queue = false;
	var $is_editpost = false;
	var $is_user = false;
	var $is_user_home = false;
	var $is_user_profile = false;
	var $is_user_submitted = false;
	var $is_user_commented = false;
	var $is_search = false;
	var $is_search_trip = false;
	var $is_feed = false;
	var $is_tagcloud = false;
	var $is_top10 = false;
	var $is_userlist = false;
	var $is_post = false;
	var $is_optionadmin = false;
	var $is_tagadmin = false;
	var $is_admin = false;

	function DB_Query ($query = '' ) {
		global $posts, $post, $user;
		
		// filter string
		$query = apply_filters($query);
		
		$this->query($query);
		
		if ($this->is_user&&isset($this->query_vars['user']))
		{
			$user = get_userdatabylogin($this->query_vars['user']);
		}
		
		if ($this->is_home || $this->is_page || $this->is_queue || $this->is_user_home || $this->is_user_submitted || $this->is_user_commented || $this->is_search || $this->is_top10 || $this->is_tagadmin || $this->is_optionadmin)
		{
			$posts = $this->get_post();
		}
	}
	
	function init () {
		$this->is_page = false;
		$this->is_home = false;
		$this->is_login = false;
		$this->is_register = false;
		$this->is_services = false;
		$this->is_about = false;
		$this->is_contact = false;
		$this->is_submit = false;
		$this->is_queue = false;
		$this->is_editpost = false;
		$this->is_user = false;
		$this->is_user_home = false;
		$this->is_user_profile = false;
		$this->is_user_submitted = false;
		$this->is_user_commented = false;
		$this->is_search = false;
		$this->is_search_trip = false;
		$this->is_feed = false;
		$this->is_tagcloud = false;
		$this->is_top10 = false;
		$this->is_userlist = false;
		$this->is_post = false;
		$this->is_optionadmin = false;
		$this->is_tagadmin = false;
		$this->is_admin = false;

		unset($this->posts);
		unset($this->query);
		unset($this->query_vars);
		unset($this->queried_object);
		unset($this->queried_object_id);
		$this->post_count = 0;
		$this->current_post = -1;
	}

	function &query($query) {
		$this->parse_query($query);
	}

	// Reparse the query vars.
	function parse_query_vars() {
		$this->parse_query('');
	}

	// Parse a query string and set query type booleans.
	function parse_query ($query) {
		
		if ( !empty($query) || !isset($this->query) ) {
			$this->init();
			parse_str($query, $qv);
			$this->query = $query;
			$this->query_vars = $qv;
		}

		if (isset($qv['q'])) {
			if (isset($qv['editpost'])) {
				$this->is_editpost = true;
			}
			$this->is_page = true;
		}
		elseif (isset($qv['login'])) {
			
			$this->is_login = true;
		}
		elseif (isset($qv['register'])) {
			
			$this->is_register = true;
		}
		elseif (isset($qv['services'])) {
			
			$this->is_services = true;
		}
		elseif (isset($qv['about'])) {
			
			$this->is_about = true;
		}
		elseif (isset($qv['contact'])) {
			
			$this->is_contact = true;
		}
		elseif (isset($qv['submit'])) {
			
			$this->is_submit = true;
		}
		elseif (isset($qv['search'])) {
			
			$this->is_search = true;

			$this->is_search_trip = true;
		}
		elseif (isset($qv['feed'])) {
			
			$this->is_feed = true;
		}
		elseif (isset($qv['tagcloud'])) {
			
			$this->is_tagcloud = true;
		}
		elseif (isset($qv['bestpost'])) {
			
			$this->is_top10 = true;
		}
		elseif (isset($qv['userlist'])) {
			
			$this->is_userlist = true;
		}
		elseif (isset($qv['post'])) {
			
			$this->is_post = true;
		}
		elseif (isset($qv['user'])) {
			$this->is_user = true;
			
			if (isset($qv['profile']))
				$this->is_user_profile = true;
			elseif (isset($qv['submitted']))
				$this->is_user_submitted = true;
			elseif (isset($qv['commented']))
				$this->is_user_commented = true;
			else
				$this->is_user_home = true;
		}
		elseif (isset($qv['tagadmin'])) {
			
			$this->is_tagadmin = true;
		}
		elseif (isset($qv['optionadmin'])) {
			
			$this->is_optionadmin = true;
		}
		elseif (isset($qv['admin'])) {
			
			$this->is_admin = true;
		}
		elseif (isset($qv['queue'])) {
			
			$this->is_queue = true;
		}
		if (  !($this->is_page || $this->is_login || $this->is_register || $this->is_services || $this->is_about || $this->is_contact || $this->is_submit || $this->is_queue || $this->is_editpost || $this->is_user || $this->is_search || $this->is_feed || $this->is_tagcloud || $this->is_top10 || $this->is_userlist || $this->is_post || $this->is_tagadmin || $this->is_optionadmin || $this->is_admin)) {
			$this->is_home = true;
		}
	}
	
	function next_post() {
        
		$this->current_post++;

		$this->post = $this->posts[$this->current_post];
		return $this->post;
	}

	function the_post() {
		global $post;
		$post = $this->next_post();
	}

	function rewind_posts() {
		$this->current_post = -1;
		if ($this->post_count > 0) {
			$this->post = $this->posts[0];
		}
	}

	function have_posts() {
		if ($this->current_post + 1 < $this->post_count) {
			return true;
		}

		return false;
	}

	// Get the Data in db for the posts
	function &get_post() {

		global $trdb;
		
		if(isset($this->query_vars['p']))
			$p = $this->query_vars['p'] - 1;
		else
			$p=0;
			
		$p = $trdb->escape($p);
		
		$place_join_request = "";
		if(isset($this->query_vars['place'])){
			$place_tag = $trdb->escape($this->query_vars['place']);
			if(is_place_exist($place_tag)) {
				$place_join_request =" INNER JOIN $trdb->post2tag d ON a.ID=d.post_id";
				$place_join_request .=" INNER JOIN $trdb->tags e ON e.tag_ID=d.tag_id AND e.tag_name='$place_tag'";
			}
			else
				$place_join_request =" INNER JOIN $trdb->post2tag d ON 1=2";
		}
		
		// Get db request for is_page
		if ( $this->is_page)
		{
			$q = $trdb->escape($this->query_vars['q']);
			$request = "SELECT ID, post_author, post_date, post_title, post_content,comment_status,post_url,favor_count,post_favor_date FROM $trdb->posts";
			$request .= " WHERE ID=".$q;
			//$request .= " AND post_status = 'publish'";
			$request .= " LIMIT 1";
		}
		
		// Get db request for is_queue
		if ($this->is_queue)
		{
			if($p==0) {
				$page_post_num = get_option('page_post_count');
				$begin_num = $p*$page_post_num;
			}
			else {
				$page_post_num = get_option('queue_post_count');
				$begin_num = $p*$page_post_num-$page_post_num+get_option('page_post_count');
			}
			$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date FROM $trdb->posts a";
			$request .= $place_join_request;
			$request .= " WHERE post_status = 'publish'";
			$request .= " AND post_favor = 'no'";
			//$request .= " ORDER BY post_date DESC";
			
			if($place_join_request==''){
				$request .= " ORDER BY a.post_date DESC,a.post_favor_date DESC";
			}
			else{
				$request .= " ORDER BY a.favor_count DESC";
			}
			$request .= " LIMIT $begin_num,$page_post_num";
		}
		
		// Get db request for is_home
		if ($this->is_home)
		{
			$page_post_num = get_option('page_post_count');
			$begin_num = $p*$page_post_num;
			$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date FROM $trdb->posts a";
			$request .= $place_join_request;
			$request .= " WHERE a.post_status = 'publish'";
			
			if($place_join_request==''){
				$request .= " AND a.post_favor = 'yes'";
				$request .= " ORDER BY a.post_favor_date DESC";
			}
			else{
				$request .= " ORDER BY a.favor_count DESC";
			}
			
			$request .= " LIMIT $begin_num,$page_post_num";
		}
		
		// Get db request for is_home
		if ($this->is_top10)
		{
			$page_post_num = get_option('page_post_count');
			$begin_num = $p*$page_post_num;
			$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date FROM $trdb->posts a";
			$request .= " WHERE a.post_status = 'publish'";
			$request .= " ORDER BY a.favor_count DESC, a.post_date";
			$request .= " LIMIT $begin_num,$page_post_num";
		}
		
		// Get db request for is_user_home
		if ($this->is_user_home)
		{
			$user_id = the_user_id(false);
			$user_name = the_user_name(false);
			if(is_username_exist($user_name)){
				$page_post_num = get_option('userpage_post_count');
				$begin_num = $p*$page_post_num;
				$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date FROM $trdb->posts a";
				$request .= " INNER JOIN $trdb->post2user b ON a.ID=b.post_id";
				$request .= " INNER JOIN $trdb->users c ON b.user_id=c.ID AND c.ID=$user_id";

				$request .= $place_join_request;
				
				$request .= " ORDER BY a.post_date DESC";
				$request .= " LIMIT $begin_num,$page_post_num";
			}
			else $request = "SELECT * FROM $trdb->posts WHERE 1=2";
		}
		
		// Get db request for is_user_submitted
		if ($this->is_user_submitted)
		{
			$user_id = the_user_id(false);
			$user_name = the_user_name(false);
			if(is_username_exist($user_name)){
				$page_post_num = get_option('userpage_post_count');
				$begin_num = $p*$page_post_num;
				$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date FROM $trdb->posts a";
				$request .= $place_join_request;
				$request .= " WHERE a.post_status = 'publish'";
				$request .= " AND a.post_author = $user_id";
				$request .= " ORDER BY a.post_date DESC";
				$request .= " LIMIT $begin_num,$page_post_num";
			}
			else $request = "SELECT * FROM $trdb->posts WHERE 1=2";
		}
		
		// Get db request for is_user_commented
		if ($this->is_user_commented)
		{
			$user_id = the_user_id(false);
			$user_name = the_user_name(false);
			if(is_username_exist($user_name)){
				$page_post_num = get_option('userpage_post_count');
				$begin_num = $p*$page_post_num;
				$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date FROM $trdb->posts a";				
				$request .= " INNER JOIN $trdb->comments b ON a.ID=b.comment_post_id AND b.comment_type='trip'";
				$request .= " INNER JOIN $trdb->users c ON b.comment_user_id=c.ID AND c.ID=$user_id";

				$request .= $place_join_request;
				
				$request .= " GROUP BY a.ID";
				$request .= " ORDER BY b.comment_date DESC";
				$request .= " LIMIT $begin_num,$page_post_num";
			}
			else $request = "SELECT * FROM $trdb->posts WHERE 1=2";
		}
		
		// Get db request for is_search
		if ($this->is_search)
		{
			$page_post_num = get_option('page_post_count');
			$begin_num = $p*$page_post_num;
			
			$keyword = trim($this->query_vars['search']);
			$keyword = apply_filters($keyword);
			$keyword = $trdb->escape($keyword);
		
			if($keyword!=''){
				if(is_place_exist($keyword)) {
					$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date from $trdb->posts a";
					$request .= " INNER JOIN $trdb->post2tag b ON a.ID=b.post_id";
					$request .= " INNER JOIN $trdb->tags c ON c.tag_id=b.tag_id AND c.tag_name = '$keyword'";
					$request .= " ORDER BY a.post_date DESC";
					$request .= " LIMIT $begin_num,$page_post_num";
				}
				else{
					$keywords = explode(" ", $keyword);
					$keyword_count = count($keywords);
					$request = "SELECT a.ID, a.post_author, a.post_date, a.post_title, a.post_content,a.comment_status,a.post_url,a.favor_count,a.post_favor_date from $trdb->posts a WHERE (";
					
					for($i=0;$i < $keyword_count;$i++)
					{
						$kw = $keywords[$i];
						if($i!=0)
							$request .= " OR ";
						$request .= "a.post_title LIKE '%$kw%' OR a.post_content LIKE '%$kw%'";
					}
					$request .= ") AND a.post_status='publish'";

					/*
					if(isset($this->query_vars['area'])){
						if($this->query_vars['area'] == 'promoted')
							$request .= " AND a.post_favor='yes'";
						elseif($this->query_vars['area'] == 'queue')
							$request .= " AND a.post_favor='no'";
					}
					*/

					$request .= " ORDER BY a.post_date DESC";
					$request .= " LIMIT $begin_num,$page_post_num";
				}
			}
			else $request = "SELECT * FROM $trdb->posts WHERE 1=2";
		}
		
		// Get db request for is_tagadmin
		if ($this->is_tagadmin)
		{
			$page_post_num = get_option('page_post_count');
			$begin_num = $p*$page_post_num;
			$request = "SELECT a.tag_ID, a.tag_name, a.tag_nicename, a.tag_description, a.tag_parent,a.tag_long,a.tag_lati,a.zoom_in,a.photo_url FROM $trdb->tags a";
			$request .= " ORDER BY a.tag_ID DESC";
			$request .= " LIMIT $begin_num,$page_post_num";
		}
		
		// Get db request for is_optionadmin
		if ($this->is_optionadmin)
		{
			$request = "SELECT option_id,option_name,option_value,option_description FROM $trdb->options";
		}
		
		$this->posts = $trdb->get_results($request);
		$this->post_count = count($this->posts);
		
		return $this->posts;
	}
}

// Make a global instance.
if (! isset($db_query)) {
    $db_query = new DB_Query($_SERVER['QUERY_STRING']);
}